/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.CuaHangDAO;
import dao.KhachHangDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.CuaHang;
import pojo.KhachHang;

/**
 *
 * @author LaiHuynh
 */
public class TimKiemKhachHang_Dialog extends javax.swing.JDialog {

    private DefaultComboBoxModel dcbm;
    private DefaultTableModel dtm;
    private int maKhachHang;

    public int getMaKhachHang() {
        return maKhachHang;
    }

    public void setMaKhachHang(int maKhachHang) {
        this.maKhachHang = maKhachHang;
    }
    
    private void hienThiDanhSach() {
         //doc du lieu
        this.dtm = new DefaultTableModel();
        List<KhachHang> ds=KhachHangDAO.layDanhSachKhachHang();
        this.dtm.addColumn("Mã Khách Hàng");
        this.dtm.addColumn("Họ Tên Khách Hàng");
        this.dtm.addColumn("Điện Thoại");
        this.dtm.addColumn("Địa Chỉ");
        this.dtm.addColumn("Email");
        this.dtm.addColumn("Tiền Nợ");
        this.dtm.addColumn("Cua Hang");
        for(int i=0; i<ds.size(); i++){ 
            KhachHang kh=ds.get(i); 
            Vector vt = new Vector();
            vt.add(kh.getMaKhachHang());
            vt.add(kh.getHoTenKhachHang());
            vt.add(kh.getDienThoai());
            vt.add(kh.getDiaChi());
            vt.add(kh.getEmail());
            vt.add(kh.getTienNo());
            CuaHang ch = kh.getCuaHang();
            vt.add(ch.getTenCuaHang());
            this.dtm.addRow(vt);
        } 
        this.jtDanhSach.setModel(dtm);
    }
    
    public void timKiem() {
        try {            
            this.dtm = new DefaultTableModel();
            Vector vt1 = (Vector) this.jcbbCuaHang.getSelectedItem();
            int maCuaHang = (Integer) vt1.get(0);
            String tenKhachHang = this.jtfTenKhachHang.getText();
            List<KhachHang> ds=KhachHangDAO.timKiemKhachHang(tenKhachHang, maCuaHang);
            this.dtm.addColumn("Mã Khách Hàng");
            this.dtm.addColumn("Họ Tên Khách Hàng");
            this.dtm.addColumn("Điện Thoại");
            this.dtm.addColumn("Địa Chỉ");
            this.dtm.addColumn("Email");
            this.dtm.addColumn("Tiền Nợ");
            this.dtm.addColumn("Cua Hang");
            for(int i=0; i<ds.size(); i++){ 
                KhachHang kh=ds.get(i); 
                Vector vt = new Vector();
                vt.add(kh.getMaKhachHang());
                vt.add(kh.getHoTenKhachHang());
                vt.add(kh.getDienThoai());
                vt.add(kh.getDiaChi());
                vt.add(kh.getEmail());
                vt.add(kh.getTienNo());
                CuaHang ch = kh.getCuaHang();
                vt.add(ch.getTenCuaHang());
                this.dtm.addRow(vt);
            } 
            this.jtDanhSach.setModel(dtm);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    /**
     * Creates new form TimKiemKhachHang_Dialog
     */
    public TimKiemKhachHang_Dialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.dcbm = new DefaultComboBoxModel();
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfTenKhachHang = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jcbbCuaHang = new javax.swing.JComboBox();
        jbtChon = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng"));

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtDanhSach);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách khách hàng theo thông tin", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));

        jLabel1.setText("Tên khách hàng:");

        jtfTenKhachHang.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfTenKhachHangCaretUpdate(evt);
            }
        });
        jtfTenKhachHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfTenKhachHangActionPerformed(evt);
            }
        });

        jLabel2.setText("Cửa Hàng:");

        jcbbCuaHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbbCuaHangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel1)
                .addGap(34, 34, 34)
                .addComponent(jtfTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jcbbCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(132, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jcbbCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtChon.setText("Chọn");
        jbtChon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtChonActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(19, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtChon)
                        .addGap(18, 18, 18)
                        .addComponent(jbtThoat)
                        .addGap(54, 54, 54))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtChon)
                    .addComponent(jbtThoat))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfTenKhachHangCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfTenKhachHangCaretUpdate
        // TODO add your handling code here:
        timKiem();

    }//GEN-LAST:event_jtfTenKhachHangCaretUpdate

    private void jtfTenKhachHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTenKhachHangActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfTenKhachHangActionPerformed

    private void jcbbCuaHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbbCuaHangActionPerformed
        // TODO add your handling code here:
        timKiem();

    }//GEN-LAST:event_jcbbCuaHangActionPerformed

    private void jbtChonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtChonActionPerformed
        // TODO add your handling code here:
        int dong = this.jtDanhSach.getSelectedRow();
        if(dong >= 0) {
            ChiTietHoaDonBanHang frm = new ChiTietHoaDonBanHang();
            this.maKhachHang = (Integer)this.jtDanhSach.getValueAt(dong, 0);
            this.setVisible(false);
        }
    }//GEN-LAST:event_jbtChonActionPerformed

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        try {
            List<CuaHang> ds=CuaHangDAO.layDanhSachCuaHang(); 
            for(int i=0; i<ds.size(); i++){ 
                CuaHang ch=ds.get(i); 
                Vector vt = new Vector();
                vt.add(ch.getMaCuaHang());
                vt.add(ch.getTenCuaHang());
                dcbm.addElement(vt);
            }
            this.jcbbCuaHang.setModel(dcbm);
            this.hienThiDanhSach();
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TimKiemKhachHang_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TimKiemKhachHang_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TimKiemKhachHang_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TimKiemKhachHang_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TimKiemKhachHang_Dialog dialog = new TimKiemKhachHang_Dialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtChon;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JComboBox jcbbCuaHang;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextField jtfTenKhachHang;
    // End of variables declaration//GEN-END:variables
}
